package mh;

import mh.map.SpellImage;
import mh.map.Sprite;

/**
 * A central reference point for creating resources useful in the game.
 * 
 * @author Gianmarco Laggia
 */
public class ResourceFactory {
	/** The single instance of this class to ever exist [singleton] */
	private static final ResourceFactory	single	= new ResourceFactory();

	/**
	 * Retrieve the single instance of this class
	 * 
	 * @return The single instance of this class
	 */
	public static ResourceFactory get() {
		return single;
	}

	/** The window the game should use to render */
	private GameWindow	window;

	/**
	 * The default contructor has been made private to prevent construction of this class anywhere externally.
	 */
	private ResourceFactory() {
	}

	/**
	 * Retrieve the game window that should be used to render the game
	 * 
	 * @return The game window in which the game should be rendered
	 */
	public GameWindow getGameWindow() {
		if (this.window == null) {
			this.window = new GameWindow();
		}

		return this.window;
	}

	/**
	 * Create or get a sprite which displays the image that is pointed to in the classpath by "ref"
	 * 
	 * @param ref
	 *            A reference to the image to load
	 * @return A sprite that can be drawn onto the current graphics context.
	 */
	public Sprite getSprite(String ref) {
		if (this.window == null) {
			throw new RuntimeException("Attempt to retrieve sprite before game window was created");
		}
		return new Sprite(this.window, ref);

	}

	/**
	 * Create or get a spell which displays the image that is pointed to in the classpath by "ref"
	 * 
	 * @param ref
	 *            A reference to the image to load
	 * @return A sprite that can be drawn onto the current graphics context.
	 */
	public SpellImage getSpell(String ref) {
		if (this.window == null) {
			throw new RuntimeException("Attempt to retrieve sprite before game window was created");
		}
		return new SpellImage(this.window, ref);

	}
}